package com.vecspace.web.servlet.model;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Value;

import com.vecspace.utils.UUIDUtil;
import com.vecspace.web.CharsetUtils;
import com.vecspace.web.HttpContextThreadLocal;

/**
 * 
 * @version: 1.0
 * @author: wanghua
 */
public class BaseController {

	public BaseController() {
		
	}
	
	public HttpServletRequest getServletRequest() {
		HttpServletRequest request = HttpContextThreadLocal.getHttpContext().getHttpRequest();		
		return request;
	}
	
	/**获取Request中的所有键值对**/
	protected Map<String, Object> getRequestParameters(){
		Map<String, Object> result = new HashMap<String, Object>();
		HttpServletRequest request = HttpContextThreadLocal.getHttpContext().getHttpRequest();
		
		Enumeration<?> enumeration = request.getParameterNames();
		while(enumeration.hasMoreElements()){
			Object obj = enumeration.nextElement();
			if(obj != null){
				String name = obj.toString();
				String val = request.getParameter(name);
				val = CharsetUtils.filterHtmlChar(val);//防范XSS攻击
				result.put(name, val);
			}
		}
		enumeration = request.getAttributeNames();
		while(enumeration.hasMoreElements()){
			Object obj = enumeration.nextElement();			
			if(obj != null){
				String name = obj.toString();
				Object val = request.getAttribute(name);
				boolean isNotParam = "org.springframework.web.context.request.async.WebAsyncManager.WEB_ASYNC_MANAGER".equals(name);
				if(!isNotParam){
					if(val instanceof String) {
						val = CharsetUtils.filterHtmlChar(val.toString());//防范XSS攻击
					}
					result.put(name, val);
				}
			}
		}
		
		String basePath = getBasePath();
		result.put("_sessionId", request.getSession().getId());
		result.put("basePath", basePath);
		result.put("_contextPath", getContextPath());
		result.put("deploy_File_Ver", deployFileVer);
		result.put("_pageUUID", UUIDUtil.createUUID());
		
		return result;
	}
	
	protected String getBasePath(){
		HttpServletRequest request = HttpContextThreadLocal.getHttpContext().getHttpRequest();
		String path = request.getContextPath();
		if(!path.startsWith("/"))path = "/"+path;
		String proxyPrefix = request.getHeader("dayu-proxy-prefix");
		if(proxyPrefix != null && proxyPrefix.length() > 0) {
			if(!proxyPrefix.startsWith("/")) {
				proxyPrefix="/" +proxyPrefix;
			}
			path = proxyPrefix+path;
		}
		String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
		return basePath;
	}
	
	protected String getContextPath(){
		HttpServletRequest request = HttpContextThreadLocal.getHttpContext().getHttpRequest();
		String path = request.getContextPath();
		if(!path.startsWith("/"))path = "/"+path;
		String proxyPrefix = request.getHeader("dayu-proxy-prefix");		
		if(proxyPrefix != null && proxyPrefix.length() > 0) {
			if(!proxyPrefix.startsWith("/")) {
				proxyPrefix="/" +proxyPrefix;
			}
			path = proxyPrefix+path;
		}
		String contextPath = path+"/";
		return contextPath;
	}
	/**返回的数据进行了html转码
	 * Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data. 
	 * You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues. 
	 * If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues. 
	 * If the parameter data was sent in the request body, such as occurs with an HTTP POST request, then reading the body directly via getInputStream or getReader can interfere with the execution of this method.
	 * @param parameterName
	 * @return 
	 */
	protected String getRequestParameter(String parameterName){
		HttpServletRequest request = HttpContextThreadLocal.getHttpContext().getHttpRequest();
		String val = request.getParameter(parameterName);
		val = CharsetUtils.filterHtmlChar(val);//防范XSS攻击
		return val;
	}
	
	/**返回的数据如果是字符串，则对其进行了html转码
	 * Returns the value of the named attribute as an Object, or null if no attribute of the given name exists. 
	 * Attributes can be set two ways. The servlet container may set attributes to make available custom information about a request. For example, for requests made using HTTPS, the attribute javax.servlet.request.X509Certificate can be used to retrieve information on the certificate of the client. Attributes can also be set programatically using ServletRequest.setAttribute. This allows information to be embedded into a request before a RequestDispatcher call. 
	 * Attribute names should follow the same conventions as package names. This specification reserves names matching java.*, javax.*, and sun.*. 
	 * @param attrName
	 * @return 
	 */
	protected Object getRequestAttribute(String attrName){
		HttpServletRequest request = HttpContextThreadLocal.getHttpContext().getHttpRequest();
		Object val = request.getAttribute(attrName);
		if(val instanceof String) {
			val = CharsetUtils.filterHtmlChar(val.toString());//防范XSS攻击
		}
		return val;
	}

	public HttpServletRequest getHttpRequest() {
		HttpServletRequest httpRequest = HttpContextThreadLocal.getHttpContext().getHttpRequest();
		
		return httpRequest;
	}

	public HttpServletResponse getHttpResponse() {
		HttpServletResponse httpResponse = HttpContextThreadLocal.getHttpContext().getHttpResponse();
		return httpResponse;
	}

	public HttpSession getHttpSession() {
		HttpServletRequest httpRequest = HttpContextThreadLocal.getHttpContext().getHttpRequest();
		HttpSession httpSession = null;
		if(httpRequest != null) {
			httpSession = httpRequest.getSession();
		}
		return httpSession;
	}
	@Value("${app.deployFileVer}")
	private String deployFileVer = "1";
}
